#
# This file is a part of the open-eBackup project.
# This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
# If a copy of the MPL was not distributed with this file, You can obtain one at
# http://mozilla.org/MPL/2.0/.
#
# Copyright (c) [2024] Huawei Technologies Co.,Ltd.
#
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
# EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
# MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
#

class Commands:
    # 执行命令，以json格式返回
    OPERATION_SCRIPT = """$Result = Invoke-Command -Session (New-PSSession -Name '{session_name}' -ConfigurationName \
    Microsoft.Exchange -ConnectionUri http://{hostname}/PowerShell/ -Authentication Kerberos -Credential (New-Object \
    -TypeName System.Management.Automation.PSCredential -ArgumentList '{username}', (ConvertTo-SecureString -String \
    '{password}' -AsPlainText -Force))) -ScriptBlock {{{cmd}}};\
    Remove-PSSession -Name '{session_name}' -Confirm:$false; $Result | ConvertTo-Json -Compress -Depth '{depth}'"""

    # 执行命令，根据执行结果返回True or False
    OPERATION_SCRIPT_OUT_NULL = """Invoke-Command -Session (New-PSSession -Name '{session_name}' -ConfigurationName \
    Microsoft.Exchange -ConnectionUri http://{hostname}/PowerShell/ -Authentication Kerberos -Credential (New-Object \
    -TypeName System.Management.Automation.PSCredential -ArgumentList '{username}', (ConvertTo-SecureString -String \
    '{password}' -AsPlainText -Force))) -ScriptBlock {{{cmd}}} | Out-Null;$Result = $?;\
    Remove-PSSession -Name '{session_name}' -Confirm:$false; $Result"""

    """
    exchange server相关命令
    """
    # 获取exchange server在线状态
    GET_EXCHANGE_ONLINE_STATUS = """Get-Service "MSExchangeIS" -ErrorAction SilentlyContinue \
    | Select Status | ConvertTo-Json -Compress -Depth 10"""

    # 获取exchange server版本
    GET_EXCHANGE_VERSION = """Get-CimInstance -ClassName win32_Product -Filter "Name='Microsoft Exchange Server'" \
    | Select Version | ConvertTo-Json -Compress -Depth 10"""

    """
    exchange server database相关命令
    """
    # 获取exchange server database guid
    GET_EXCHANGE_DATABASE_GUID = """Get-MailboxDatabase -Identity '{database_name}' | Select-Object Guid"""

    # 设置数据库可覆盖恢复属性为true
    SET_EXCHANGE_ALLOW_FILE_RESTORE_TRUE = """Set-MailboxDatabase -Identity '{database_name}' -AllowFileRestore $true"""

    # 获取exchange server 活动数据库所在server名称
    GET_EXCHANGE_DATABASE_SERVER = """Get-MailboxDatabase -Identity '{database_name}' | Select-Object Server"""

    # 获取exchange server 活动数据库edb_path和log_path
    GET_EXCHANGE_DATABASE_LOG_AND_EDB_PATH = """Get-MailboxDatabase -Identity '{database_name}' \
    | Select-Object EdbFilePath, LogFolderPath"""

    # 获取exchange server 活动数据库log文件前缀
    GET_EXCHANGE_DATABASE_LOG_FILE_PREFIX = """Get-MailboxDatabase -Identity '{database_name}' \
    | Select-Object LogFilePrefix"""

    # 获取exchange server database副本信息列表
    GET_EXCHANGE_DATABASE_COPIES = """Get-MailboxdatabaseCopyStatus -Identity '{copy_name}' \
    | Select-Object Name, Status"""

    # 暂停被动副本复制
    SUSPEND_EXCHANGE_DATABASE_COPY = """Suspend-MailboxDatabaseCopy -Identity '{database_copy}' -Confirm:$False"""

    # 恢复被动副本复制，并更新种子
    UPDATE_EXCHANGE_DATABASE_COPY = """Update-MailboxDatabaseCopy -Identity '{database_copy}' -DatabaseOnly \
    -DeleteExistingFiles -Force -Confirm:$False"""

    # 卸载数据库
    DISMOUNT_EXCHANGE_DATABASE = """Dismount-Database -Identity '{database_name}' -Confirm:$false"""

    # 挂载数据库
    MOUNT_EXCHANGE_DATABASE = """Mount-Database -Identity '{database_name}' -Confirm:$false -Force"""

    # 查询数据库副本信息
    GET_MAILBOX_DATABASE_COPY_STATUS = """Get-MailboxdatabaseCopyStatus -Identity '{db_name}' |Select-Object Name, \
    MailboxServer, Status, ActiveDatabaseCopy, ActiveCopy, ActivationPreference, ContentIndexState"""

    # 创建数据库
    CREATE_EXCHANGE_DATABASE = """New-MailboxDatabase -Name '{database_name}' -Server '{server_name}' \
    -EdbFilePath '{edb_path}' -LogFolderPath '{log_path}'  -Confirm:$false"""

    # 获取exchange server 所有数据库副本
    GET_ALL_EXCHANGE_DATABASE_COPIES = """Get-MailboxDatabase | Select-Object DatabaseCopies, Server"""

    """
    exchange server mailbox相关命令
    """
    # 校验邮箱备份权限
    CHECK_MAILBOX_BACKUP_PERMISSION = """Get-RoleGroup -Identity 'Databackup'"""

    # 查询指定服务节点的数据库信息
    GET_MAILBOX_DATABASE_BY_SERVER = """Get-MailboxDatabase -Server '{server}' |Select-Object Name, \
    Server, Recovery"""

    # 获取公共数据库列表 只有Exchange2010可以创建该数据库
    GET_PUBLIC_FOLDER_DATABASE_BY_SERVER = """Get-PublicFolderDatabase -Server '{server}' |Select-Object Name, \
    Server, Recovery"""

    # 查询数据基本信息，edb路径，日志路径，guid
    GET_MAILBOX_DATABASE_BASE_INFO = """Get-MailboxDatabase -Identity '{db_name}' |Select-Object EdbFilePath, \
     LogFolderPath, Guid, LogFilePrefix"""

    # 根据数据库名查询邮箱
    GET_MAILBOX_LIST_BY_DATABASE = """Get-Mailbox -Database '{database_name}' -ResultSize Unlimited | Select-Object \
     -First {first} -Skip {skip} ExchangeGuid, Name, DisplayName, Alias, PrimarySmtpAddress, RecipientType"""

    # 判断邮箱是否存在
    IS_MAILBOX_EXIST = """Get-Mailbox -Identity "{mailbox_name}" """

    # 查询所有exchange服务器
    GET_ALL_EXCHANGE_SERVER = """Get-ExchangeServer"""

    # 查询DAG组
    GET_DATABASE_AVAILABLE_GROUP = """Get-DatabaseAvailabilityGroup"""

    # 查询指定exchange服务器
    GET_SPECIFIED_EXCHANGE_SERVER = """Get-ExchangeServer -identity {server_name}"""

    # 获取DAG组下所有可以备份的数据库副本信息
    GET_MOUNTED_DATABASES_BY_SERVER = """Get-MailboxDatabaseCopyStatus -Server '{server}' |\
     Select-Object Status, DatabaseName"""

    # 创建RDB数据库
    CREATE_RDB_DATABASE = """New-MailboxDatabase -Recovery -Name '{database_name}' -Server '{server}' -EdbFilePath \
    '{edb_file_path}' -LogFolderPath '{log_folder_path}'"""

    # 恢复邮箱
    MAILBOX_RESTORE_REQUEST = """New-MailboxRestoreRequest -Name '{request_name}' -SourceDatabase '{database_name}' \
    -SourceStoreMailbox '{source_mailbox}' -TargetMailbox {target_mailbox} -AllowLegacyDNMismatch"""

    # 删除数据库
    REMOVE_DATABASE = """Remove-MailboxDatabase -Identity '{database_name}' -Confirm:$false"""

    # 删除邮箱恢复请求
    REMOVE_MAILBOX_RESTORE_REQUEST = """Remove-MailboxRestoreRequest -Identity '{request_id}' -Confirm:$false"""

    # 查询恢复请求状态
    GET_MAILBOX_RESTORE_REQUEST = """Get-MailboxRestoreRequest -Name {request_name} | Select-Object Name, \
    RequestGuid, Status, TargetMailbox"""

    # 获取当前节点上的数据库
    GET_DATABASE_IN_SERVER = """Get-MailboxDatabase -Server {server} """

    # 获取Windows版本
    GET_WINDOWS_SERVER_VERSION = "Get-WmiObject -Class Win32_OperatingSystem | Select-Object BuildNumber, Version \
     | ConvertTo-Json -Compress -Depth 10"
